package ytdp.careagle.com.ytdp.myview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wx.wheelview.adapter.MyWheelAdapter;
import com.wx.wheelview.widget.WheelView;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.utils.MyToast;

/**
 * Created by admin on 2017/10/17.
 */

public class SelectTimeView extends RelativeLayout {
    @Bind(R.id.tv_cancel)
    TextView tvCancel;
    @Bind(R.id.tv_ok)
    TextView tvOk;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.main_wheelview)
    WheelView mainWheelview;
    @Bind(R.id.sub_wheelview)
    WheelView subWheelview;
    @Bind(R.id.ll_content)
    LinearLayout llContent;
    @Bind(R.id.ll_viewGroup)
    LinearLayout llViewGroup;
    @Bind(R.id.ll_gen)
    RelativeLayout llGen;
    private Activity activity;
    private int year;
    private int month;
    private ArrayList<Object> years;
    private ArrayList<Object> months;
    private float bottom;
    private SelecedListener listener;

    public SelectTimeView(Context context) {
        this(context, null);
    }

    public SelectTimeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        activity = (Activity) context;
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_select_time, null);
        addView(inflate);
        ButterKnife.bind(this);
        initView();
        tvTitle.setText("上牌时间");
    }

    @OnClick({R.id.tv_cancel, R.id.tv_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ok:
                if (listener != null) {
                    Object mainWheelviewSelectedItem = mainWheelview.getSelectionItem();
                    Object subWheelviewSelectedItem = subWheelview.getSelectionItem();
                    String year = mainWheelviewSelectedItem.toString();
                    year = year.replaceAll("年", "");
                    String month = subWheelviewSelectedItem.toString();
                    month = month.replaceAll("月", "");
                    listener.selected(year, month);
                }
                closed();
                break;
            case R.id.tv_cancel:
                closed();
                break;
        }
    }

    public void closed() {
        animate().translationYBy(getTranslationY()).translationY(getHeight() * 2).setDuration(200).start();
    }

    public void initView() {
        bottom = getResources().getDimension(R.dimen.dp15);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) llViewGroup.getLayoutParams();
        layoutParams.setMargins(0, (int) bottom, 0, 0);
        initDatas();
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        mainWheelview.setSkin(WheelView.Skin.Holo);
        mainWheelview.setWheelSize(7);
        subWheelview.setWheelSize(7);
        mainWheelview.setWheelAdapter(new MyWheelAdapter(activity));
        subWheelview.setWheelAdapter(new MyWheelAdapter(activity));
        subWheelview.setSkin(WheelView.Skin.Holo);
        mainWheelview.setStyle(style);
        style.selectedTextSize = 30;
        style.textSize = 24;
        subWheelview.setStyle(style);
        mainWheelview.setWheelData(years);
        subWheelview.setWheelData(months);
    }

    public void setSelected(int lastYear, int lastMonth) {
        if (lastYear == -1) {
            mainWheelview.setSelection(years.size());
        } else {
            mainWheelview.setSelection(lastYear - 1970);
        }
        if (lastMonth == -1) {
            subWheelview.setSelection(this.month - 1);
        } else {
            subWheelview.setSelection(lastMonth - 1);
        }
    }

    private void initDatas() {
        Calendar now = Calendar.getInstance();
        year = now.get(Calendar.YEAR);
        month = now.get(Calendar.MONTH) + 1;
        years = new ArrayList<>();
        for (int i = 1970; i <= year; i++) {
            years.add(i + "年");
        }
        months = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            months.add(i + "月");
        }
    }

    public void setListener(SelecedListener listener) {
        this.listener = listener;
    }

    public interface SelecedListener {
        void selected(String year, String month);
    }
}
